import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test(self):
        self.browser.get("http://nihao/admin/account/login.html")
        sleep(1)
        # 定位用户名输入框输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('nihao')
        # 定位密码输入框输入密码
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        # 点击登录按钮
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        # 点击商品
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        # 查找子框架
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe1)
        # 定位商品名称输入框
        self.browser.find_element(By.ID, 'keyword').send_keys('文具')
        # 点击查询按钮
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
         # 定位商品列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        is_not_bug=True
        if len(tr_list) == 0:
            print('没有数据')
        else:
               for tr in tr_list:
                   # 在tr查找td
                    td = tr.find_element(By.XPATH, './td[2]/div')
                    title = td.text
                    # print(td.text)
                    if '文具' not in title:
                        print('有bug')
                        is_not_bug=False
                        break
        if is_not_bug:
            print('没有bug')
        #退出子框架
        self.browser.switch_to.default_content()
        def test_add_shop(self):
                """
            添加商品测试
             """
        # 点击商品按钮
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位发布商品按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        # 定位商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 定位商品名称输入框并输入商品名称
        self.browser.find_element(By.NAME, 'name').send_keys('文具哦')
        sleep(1)
        # 定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        # 定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        # 定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        # 定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        # 定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        # 定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '//html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[3]').click()
        sleep(1)
        
        # 定位商品主图的添加按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        self.test_upload_img('D:\\rjcs\image0.png', '//*[@id="layui-layer-iframe2"]')
         # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到商品发布子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        
        # 定位商品轮播图的添加按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('D:\\rjcs\image1.png', '//*[@id="layui-layer-iframe3"]')
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到商品发布按钮子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        
         # 定位多两张商品轮播图的添加按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('D:\\rjcs\image2.png', '//*[@id="layui-layer-iframe4"]')
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到商品发布按钮子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('D:\\rjcs\image3.png', '//*[@id="layui-layer-iframe5"]')
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到商品发布按钮子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        
        # 切换到价格设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        # 定位市场价格输入框并输入价格
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('222')
        sleep(1)
        # 定位价格输入框并输入价格
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('888')
        sleep(1)
        # 定位成本输入框并输入成本
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('555')
        sleep(1)
        # 定位库存输入框并输入库存
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('111')
        sleep(1)
        # 切换到销售设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        sleep(2)
        # 定位配送方式的快递发货并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
        sleep(1)
        # 返回到商品子框架
        self.browser.switch_to.parent_frame()
        # 点击保存按钮
        self.browser.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()
        sleep(2)
        #退出子框架
        self.browser.switch_to.default_content()
    def test_sj_shop(self):
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 点击商品按钮
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        
        # 定位仓库中商品切换标签页并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        # 接收商品列表
        goods_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        sleep(2)
        if len(goods_list) > 0:
            # 定位第一个商品上架按钮并点击
            goods_list[0].find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[2]').click()
            sleep(2)
            # 定位缺人上架按钮并点击
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
        
    def test_edit_shop(self):
        """
            编辑商品测试
             """
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 点击商品按钮
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位商品编辑按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
        sleep(2)
        # 切换到商品编辑的子框架
        # 查找编辑窗口的iframe（使用通用定位，不要用固定ID）
        iframes = self.browser.find_elements(By.XPATH, '//iframe[contains(@id, "layui-layer-iframe")]')
        print(f"找到 {len(iframes)} 个编辑iframe")

        if len(iframes) > 0:
            iframe_edit = iframes[-1]
            print(f"切换到iframe: {iframe_edit.get_attribute('id')}")
            self.browser.switch_to.frame(iframe_edit)
            sleep(2)
        else:
            print("错误：未找到编辑窗口的iframe")
            return
        
        # 定位商品名称输入框并修改商品名称
        self.browser.find_element(By.NAME, 'name').clear()
        self.browser.find_element(By.NAME, 'name').send_keys('铅笔哦')
        sleep(2)
        #定位第二个商品分类下拉项并修改点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
        sleep(2)
        #定位第三个商品分类下拉项并修改点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(2)
        #切换到价格设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        #定位价格输入框并修改价格
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('999')
        sleep(1)
        # 定位库存输入框并修改库存
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').clear()
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('333')
        sleep(1)
        # 切换到销售设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        sleep(2)
        
        # 返回到商品子框架
        self.browser.switch_to.parent_frame()
        # 点击保存按钮
        self.browser.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()
        sleep(2)
        #退出子框架
        self.browser.switch_to.default_content()
    def test_upload_img(self, img_path, xpath_path):
        """
        测试上传商品图片
             """
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 定位图片上传子框架并进入子框架
        iframe_img = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(iframe_img)
        sleep(2)
        # 定位图片上传输入框并上传图片
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        # 选择第一张图片
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(2)
        # 点击使用图片按钮
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
    def TC(self):
        #退出子框架
        self.browser.switch_to.default_content()
        sleep(2)
        #定位用户信息
        df=self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        # 鼠标悬停在用户信息上
        ActionChains(self.browser).move_to_element(df).perform()
        sleep(2)
        # 点击退出
        self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
        
        
        
        
if __name__ == '__main__' :
    lk = LikeShopTest()
    lk.test()
    lk.test_sj_shop()
    lk.test_edit_shop()
    lk.TC()
  
